###########################################################################
# Lipps & Sczepanski & Malet
# Understanding Preferences over Borders
# International Studies Quaterly

# Appendix: Perceptions of country similarity  --------------------------
###########################################################################

rm(list = ls())

packages <- c("tidyverse", "cregg", "tidytext", "ggplot2", "xtable")

for (package in packages) {
  if (!requireNamespace(package, quietly = TRUE)) {
    install.packages(package, dependencies = TRUE)
  }
}

library(tidyverse)
library(ggplot2)
library(cregg)
library(xtable)
library(viridis)
library(ggpubr)

#Load data
dat_long <- read.csv("survey_dat_clean_conjoint.csv", na.strings=c("","NA"), row.names = 1)
dat <- read.csv("survey_dat_clean.csv", na.strings=c("","NA"), row.names = 1)

#recode
dat_long$border <- as.factor(dat_long$border)
dat_long$country <- as.factor(dat_long$country)
dat_long$aim <- as.factor(dat_long$aim)
dat_long$gender <- as.factor(dat_long$gender)
dat_long$migration_background <- as.factor(dat_long$migration_background)


#make labels for plots
labels<- as_labeller(c("border" = "Measure", "aim"="Aim", "country"="Country", "sim_cat" = "Country"))

#### TABLES AND FIGURES ####--------------------------------------------------------

#Figure D1: Perception of country similarity with Germany by vote intention
dat %>% dplyr::select(id, difference_countries_1, difference_countries_2, difference_countries_3, difference_countries_4) -> percsim
names(percsim)[2:5] <- c("France", "Czech Republic", "Poland", "Switzerland")

percsim %>% pivot_longer(cols=2:5, names_to="country") %>%
  left_join(dat[,c("id", "vote_intention", "east")], by="id") -> percsim
names(percsim)[3] <- "country_sim"

percsim %>% mutate(vote_intention = case_when(vote_intention=="Don't know" ~ "Other",
                                              vote_intention=="No show" ~ "Other",
                                              TRUE ~ vote_intention)) -> percsim

percsim$vote_intention <- ordered(percsim$vote_intention, levels = c("The Left", "SPD", "Greens", "FDP", "CDU/CSU", "AfD", "Other"))

summary_sim<- percsim %>%  
  group_by(country, vote_intention) %>% 
  summarise(mean_sim = mean(country_sim, na.rm=TRUE), 
            sd_sim = sd(country_sim, na.rm=TRUE),
            n = n()) %>% 
  mutate(con_upper = mean_sim+qt(1 - (0.05 / 2), n - 1)*(sd_sim/sqrt(n)),
         con_lower = mean_sim-qt(1 - (0.05 / 2), n - 1)*(sd_sim/sqrt(n))) 

pdf(file = "fig_D1.pdf",width = 8,height = 5,onefile=F)

ggplot(summary_sim, aes(y=mean_sim, x=vote_intention, color=country)) + 
  geom_point(size=1.5) +
  geom_errorbar(aes(ymin=con_lower, ymax=con_upper), linewidth=1) + 
  scale_color_manual(values=c("red3", "forestgreen", "darkgoldenrod1", "deepskyblue3")) +
  scale_y_continuous(breaks=c(4,4.5,5,5.5,6,6.5)) +
  guides(color = FALSE) + 
  labs(y="Rating of similarity", x="") +
  theme_bw() +
  theme(legend.position="top",
        axis.text.y = element_text(size = 11),
        axis.text.x = element_text(size=11, angle=45, hjust=1),
        axis.title.y= element_text(size = 14),
        axis.title.x = element_text(size=14)) +
  facet_grid(~country)

dev.off()

#Figure D2: Perception of country similarity with Germany by East/West
summary_simeast<- percsim %>%  
  group_by(country, east) %>% 
  summarise(mean_sim = mean(country_sim, na.rm=TRUE), 
            sd_sim = sd(country_sim, na.rm=TRUE),
            n = n()) %>% 
  mutate(con_upper = mean_sim+qt(1 - (0.05 / 2), n - 1)*(sd_sim/sqrt(n)),
         con_lower = mean_sim-qt(1 - (0.05 / 2), n - 1)*(sd_sim/sqrt(n)))

summary_simeast$east <- as.factor(summary_simeast$east)

pdf(file = "fig_D2.pdf",width = 8,height = 5,onefile=F)

ggplot(summary_simeast, aes(y=mean_sim, x=east, color=country)) + 
  geom_point(size=1.5) +
  geom_errorbar(aes(ymin=con_lower, ymax=con_upper), linewidth=1) + 
  scale_color_manual(values=c("red3", "forestgreen", "darkgoldenrod1", "deepskyblue3")) +
  scale_y_continuous(breaks=c(4,4.5,5,5.5,6,6.5)) +
  guides(color = FALSE) + 
  labs(y="Rating of similarity", x="") +
  theme_bw() +
  theme(legend.position="top",
        axis.text.y = element_text(size = 11),
        axis.text.x = element_text(size=11, angle=45, hjust=1),
        axis.title.y= element_text(size = 14),
        axis.title.x = element_text(size=14)) +
  facet_grid(~country)

dev.off()

#Figure D3: by similarity score
dat_long <- dat_long %>% left_join(percsim[,c(1,2,3)], by=c("country", "id"))

dat_long <- dat_long %>% mutate(sim_cat = as.factor(case_when(country_sim>6 ~ "rather similar",
                                                              country_sim<4 ~ "rather dissimilar",
                                                              country_sim>=4 & country_sim<6 ~ "somewhat similar")))
dat_long$sim_cat <- ordered(dat_long$sim_cat, levels=c("rather similar", "somewhat similar", "rather dissimilar"))

results_sim1 <- cregg::cj(chosen ~ border+aim+sim_cat, data=dat_long,
                          id=~id, estimate="mm", h0=5)

pdf(file = "fig_D3.pdf",width = 8,height = 5,onefile=F)

ggplot(results_sim1)+
  geom_point(aes(x=estimate, y=level), size=2)+
  geom_errorbar(aes(xmin=lower, xmax=upper, y=level), width=0, linewidth=1)+
  facet_grid(feature~., scales = "free", labeller = labels)+
  geom_vline(xintercept = 0.5, linetype="dotted")+
  labs(x="Marginal Means = Rating", y="")+
  theme_minimal() +
  theme(
    axis.text.y.left = element_text(size = 16),
    axis.text.x = element_text(size=16),
    axis.title.y= element_text(size = 16),
    axis.title.x = element_text(size=16),
    strip.text.y.right = element_text(size=16))

dev.off()

#interact similarity
#result_int3 <- cj(dat_long, chosen ~ border+aim, id = ~id, estimate = "mm", by = ~sim_cat)

#ggplot(result_int3, aes(color=feature))+
#  geom_point(aes(x=estimate, y=level), size=1.5)+
#  geom_errorbar(aes(xmin=lower, xmax=upper, y=level), width=0, linewidth=1)+
#  facet_grid(feature~., scales = "free", labeller = labels)+
#  geom_vline(xintercept = 0.5, linetype="dotted")+
#  labs(x="Marginal Means", y="")+
#  scale_color_manual(values=c("deepskyblue3", "darkgoldenrod1")) +
#  guides(color=FALSE) +
#  theme_minimal() +
#  theme(
#    axis.text.y.left = element_text(size = 12),
#    axis.text.x = element_text(size=12),
#    axis.title.y= element_text(size = 12),
#    axis.title.x = element_text(size=12),
#    strip.text.y.right = element_text(size=12)) +
#  facet_wrap(~BY, ncol = 3L)

#Figure D4: similarity score by vote intention
dat_long$vote_intention <- ordered(dat_long$vote_intention, levels = c("The Left", "SPD", "Greens", "FDP", "CDU/CSU", "AfD", "Other", "No show", "Don't know"))

result_int4 <- cj(dat_long, chosen ~ border+aim+sim_cat, id = ~id, estimate = "mm", by = ~vote_intention)

pdf(file = "fig_D4.pdf",width = 8,height = 5,onefile=F)

result_int4 %>% filter(vote_intention!="Don't know", vote_intention!="No show", vote_intention!="Other") %>%
  ggplot(aes(color=vote_intention))+
  geom_point(aes(x=estimate, y=level), size=1.5, position = position_dodge(0.5))+
  geom_errorbar(aes(xmin=lower, xmax=upper, y=level), width=0, linewidth=1, position = position_dodge(0.5))+
  facet_grid(feature~., scales = "free", labeller = labels)+
  geom_vline(xintercept = 0.5, linetype="dotted")+
  labs(x="Marginal Means", y="", color="Vote intention")+
  scale_color_manual(values=c("violetred1", "red3", "forestgreen", "darkgoldenrod1", "black", "deepskyblue3"))+
  theme_minimal() +
  theme(
    axis.text.y.left = element_text(size = 12),
    axis.text.x = element_text(size=12),
    axis.title.y= element_text(size = 12),
    axis.title.x = element_text(size=12),
    strip.text.y.right = element_text(size=12))

dev.off()
